<?php
/**
 * Created by PhpStorm.
 * User: zhj
 * Date: 2019/1/16
 * Time: 14:14
 */

namespace app\index\controller;
use app\common\validate\Address;

class User extends IndexBase{


    /**
     * 用户登录
     */
    public function login()
    {
        if(IS_GET){
            return $this->fetch('login/index');
        }
        $data = input('param.');
        $vilidate = new \app\common\validate\User();
        $res = $vilidate->scene('login')->check($data);
        if(!$res){
            $this->ajaxReturn(0,[],$vilidate->getError());
        }
        $user = new \app\common\model\User();
        $data['password'] =  md5($data['password'].config('password_token'));
        $loginRes = $user->getUserInfo($data);
        if($loginRes){
            session('userid',$loginRes['id']);
            session('userinfo',$loginRes);
            $this->ajaxReturn(1,['url'=>url('/index/index/index')],'登录成功！');
        }else{
            $this->ajaxReturn(0,[],'登录失败！');
        }
    }

    /**
     * 用户注册
     */

    public function register()
    {
        if(IS_GET){
            return $this->fetch('register/index');
        }
    }

    public function register2()
    {

        $data = input('param.');

        $vilidate = new \app\common\validate\User();
        $vilidate->rule('password','require|confirm|length:6,30');
        $res = $vilidate->scene('add')->check($data);
        if(!$res){
            $this->ajaxReturn(0,[],$vilidate->getError());
        }
        $user = new \app\common\model\User();
        $saveData = $user->add($data);
        if($saveData){
            $this->ajaxReturn(1,['url'=>url('/index/index/login')],'注册成功！');
        }else{
            $this->ajaxReturn(0,[],'注册失败！');
        }
    }

    public function address()
    {
        $uid =  userLogin();
        if(!$uid){
            $this->jump(RESULT_REDIRECT, '未登录', url('index/user/login'));
        }
        return $this->fetch('address');
    }

    public function add_address()
    {
        $data = input('param.');
        $data['userid'] = session('userid');

        $validate = new Address();
        if(!$validate->check($data)){
            $this->ajaxReturn(0,[],$validate->getError());
        }
        $address =  new \app\common\model\Address();
        $res = $address->addData($data);
        if($res){
            $this->ajaxReturn(1,[],'添加地址成功~');
        }
    }
}
